package com.pig4cloud.pig.module.controller.electric;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.DateUtils;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.electric.ElectricOrder;
import com.pig4cloud.pig.module.service.electric.IElectricOrderService;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 电充值订单Controller
 *
 * @author ruoyi
 * @date 2022-09-02
 */
@Validated
@Api(value = "电充值订单控制器", tags = {"电充值订单管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/devices/order")
public class ElectricOrderController {

    private final IElectricOrderService electricOrderService;
    /**
     * 查询电充值订单列表
     */
    @ApiOperation("查询电充值订单列表")
    @PreAuthorize("@pms.hasPermission('module:order:list')")
    @GetMapping("/list")
    public TableDataInfo<ElectricOrder> list(@Validated ElectricOrder electricOrder) {
        QueryWrapper<ElectricOrder> qw = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(electricOrder.getDeviceSn())){
            qw.eq("device_sn",electricOrder.getDeviceSn());
        }
        if (electricOrder.getOrderNo()!=null){
            qw.eq("order_no",electricOrder.getOrderNo());
        }
        if(StrUtil.isNotEmpty(electricOrder.getBeginDate())){
            qw.between("create_time",electricOrder.getBeginDate() + " 00:00:00",electricOrder.getEndDate() + " 23:59:59");
        }
        qw.orderByDesc("create_time");

        TableDataInfo<ElectricOrder> td = PageUtils.buildDataInfo(electricOrderService.page(PageUtils.buildPage(),qw));
        List<ElectricOrder> list = td.getData();
        list.forEach(order->{
            if(StrUtil.isNotEmpty(order.getNickName())){
                order.setNickName(EmojiParser.parseToUnicode(order.getNickName()));
            }
        });
        return td;
    }

    /**
     * 导出电充值订单列表
     */
    @ApiOperation("导出电充值订单列表")
    @PreAuthorize("@pms.hasPermission('module:order:export')")
    @SysLog("电充值订单")
    @GetMapping("/export")
    public List export(@Validated ElectricOrder electricOrder) {
        QueryWrapper<ElectricOrder> qw = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(electricOrder.getDeviceSn())){
            qw.eq("device_sn",electricOrder.getDeviceSn());
        }
        if (electricOrder.getOrderNo()!=null){
            qw.eq("order_no",electricOrder.getOrderNo());
        }
        if(StrUtil.isNotEmpty(electricOrder.getBeginDate())){
            qw.between("create_time",electricOrder.getBeginDate() + " 00:00:00",electricOrder.getEndDate() + " 23:59:59");
        }
        qw.orderByDesc("create_time");
        List<ElectricOrder> list = electricOrderService.list(qw);
        for (ElectricOrder list1:list){
            if (list1.getPayType()==0){
                list1.setPayTypeName("微信支付");
            }
            if (list1.getPayType()==1){
                list1.setPayTypeName("农商行支付");
            }
            if(list1.getCreateTime()!=null){
                list1.setCreateTimeStr(DateUtils.getTime(list1.getCreateTime()));
            }
            if(list1.getStatus()==0){
                list1.setStatusName("未支付");
            }
            if(list1.getStatus()==1){
                list1.setStatusName("支付中");
            }
            if(list1.getStatus()==2){
                list1.setStatusName("已支付");
            }
            if(list1.getStatus()==3){
                list1.setStatusName("充电中");
            }
            if(list1.getStatus()==4){
                list1.setStatusName("已发起退款");
            }
            if(list1.getStatus()==5){
                list1.setStatusName("退款完成");
            }
            if(list1.getStatus()==6){
                list1.setStatusName("退款失败");
            }
            if(list1.getStatus()==7){
                list1.setStatusName("订单已完成");
            }
            if(list1.getStatus()==8){
                list1.setStatusName("订单取消");
            }
        }
        // ExcelUtil<ElectricOrder> util = new // ExcelUtil<ElectricOrder>(ElectricOrder.class);
        return  list;// util.exportExcel(list, "电充值订单");
    }

    /**
     * 获取电充值订单详细信息
     */
    @ApiOperation("获取电充值订单详细信息")
    @PreAuthorize("@pms.hasPermission('module:order:query')")
    @GetMapping("/{id}")
    public R<ElectricOrder> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") String id) {
        ElectricOrder electricOrder = electricOrderService.getById(id);
        return R.ok(electricOrder);
    }

    /**
     * 新增电充值订单
     */
    @ApiOperation("新增电充值订单")
    @PreAuthorize("@pms.hasPermission('module:order:add')")
    @SysLog("电充值订单")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody ElectricOrder electricOrder) {
        return R.okByRow(electricOrderService.save(electricOrder) ? 1 : 0);
    }

    /**
     * 修改电充值订单
     */
    @ApiOperation("修改电充值订单")
    @PreAuthorize("@pms.hasPermission('module:order:edit')")
    @SysLog("电充值订单")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody ElectricOrder electricOrder) {
        return R.okByRow(electricOrderService.updateById(electricOrder) ? 1 : 0);
    }


    /**
     * 删除电充值订单
     */
    @ApiOperation("删除电充值订单")
    @PreAuthorize("@pms.hasPermission('module:order:remove')")
    @SysLog("电充值订单")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable String[] ids) {
        return R.okByRow(electricOrderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
